<template>
  <div class="app-container">
    <!-- 搜索行 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="`${$schoolName ? $schoolName : '学校'}名称`" prop="schoolName">
        <el-input v-model="queryParams.schoolName" :placeholder="`请输入${$schoolName ? $schoolName : '学校'}名称`" clearable
                  @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="levelName">
        <el-select filterable v-model="queryParams.levelId" placeholder="级别" clearable>
          <el-option v-for="item in level" :key="item.levelId" :label="item.levelName" :value="item.levelId"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-preventReClick type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button v-preventReClick icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 操作行 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-preventReClick type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-preventReClick type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete"
        >删除
        </el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- tab -->
    <!-- // 0.启用 1.禁用 2.待提交 3.待审核 4.已驳回 5.已到期 -->
    <div class="commonMenu">
      <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" @select="handleSelect">
        <el-menu-item index="">全部</el-menu-item>
        <el-menu-item index="0">已启用</el-menu-item>
        <el-menu-item index="1">已停用</el-menu-item>
        <el-menu-item index="5">已到期</el-menu-item>
      </el-menu>
    </div>
    <!-- 表格 -->
    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="`${$schoolName ? $schoolName : '学校'}编号`" align="center" prop="schoolNum"/>
      <el-table-column width="100" :label="`${$schoolName ? $schoolName : '学校'}logo`" align="center" prop="schoolPic">
        <template slot-scope="scope">
          <el-image style="width: 80px; height: auto" :src="scope.row.schoolPic"
                    :preview-src-list="[scope.row.schoolPic]"
          >
          </el-image>
        </template>
      </el-table-column>
      <el-table-column :label="`${$schoolName ? $schoolName : '学校'}名称`" align="center" prop="schoolName"/>
      <el-table-column label="办学层次" align="center" prop="hierarchy"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <!-- // 0.启用 1.禁用 2.待提交 3.待审核 4.已驳回 5.已到期 -->
          <el-tag type="success" v-if="scope.row.status == 0">启用</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 1">禁用</el-tag>
          <el-tag type="info" v-if="scope.row.status == 5">已到期</el-tag>
          <el-tag type="warning" v-if="scope.row.status == 3">待审核</el-tag>
          <el-tag type="danger" v-if="scope.row.status == 4">已驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="级别" align="center" prop="levelName"/>
      <el-table-column label="添加时间" align="center" prop="createTime" min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime).slice(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="有效期至" sortable align="center" prop="endTime" min-width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime).slice(0, 10) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="principal">
      </el-table-column>
      <el-table-column label="电话" align="center" prop="tel">
      </el-table-column>
      <el-table-column label="管理员数量" align="center" prop="createTime">
        <template slot-scope="scope">
          <adminDia @refresh="getList()" :adminNum="scope.row.sysUsers.length" :schoolId="scope.row.schoolId">
          </adminDia>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="120">
        <template slot-scope="scope">
          <el-button v-preventReClick size="mini" type="text" @click="handleUpdate('detial', scope.row)"
                     icon="el-icon-view"
          >
            详情
          </el-button>
          <!-- 更多下拉按钮  -->
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-button slot="reference" type="text" size="mini">
                更多<i class="el-icon-arrow-down"></i>
              </el-button>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <el-button v-preventReClick size="mini" type="text" icon="el-icon-info"
                           @click="handleUpdate('edit', scope.row)"
                >编辑基本信息
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-preventReClick v-if="scope.row.status != 3" size="mini" type="text" icon="el-icon-s-custom"
                           @click="showExamine(scope.row)"
                >权限信息
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-preventReClick v-if="scope.row.status == 4" size="mini" type="text"
                           @click="handleReject(scope.row)" icon="el-icon-view"
                >
                  驳回原因
                </el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button v-preventReClick v-if="scope.row.status == 0 || scope.row.status == 1" size="mini"
                           type="text" icon="el-icon-open" @click="handleSwitch(scope.row)"
                >{{
                    scope.row.status == 0 ? '停用' :
                      '启用'
                  }}
                </el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"
    />
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" title="权限信息"
               :visible.sync="showExamineDialog" width="500px" append-to-body
    >
      <el-form @submit.native.prevent ref="examineForm" :model="examineForm" :rules="examineFormRules"
               label-width="80px"
      >
        <el-form-item label="学校名称" prop="schoolName">
          <div>{{ examineForm.schoolName }}</div>
        </el-form-item>
        <el-form-item label="学校logo" prop="schoolPic">
          <div>
            <el-image width="120px" height="120px" :src="examineForm.schoolPic"
                      :preview-src-list="[examineForm.schoolPic]"
            >
            </el-image>
          </div>
        </el-form-item>
        <el-form-item label="级别" prop="levelId">
          <el-select filterable class="level_add" v-model="examineForm.levelId" placeholder="级别" clearable>
            <el-option v-for="item in level" :key="item.levelId" :label="item.levelName" :value="item.levelId"/>
          </el-select>
        </el-form-item>
        <div style="display: flex">
          <el-form-item label="起止日期" prop="startTime">
            <el-date-picker disabled class="startTime_add" :picker-options="pickerOptionsEnd"
                            v-model="examineForm.startTime" type="date" clearable placeholder="开始日期"
            >
            </el-date-picker>
          </el-form-item>
          <span style="margin: 7px 10px 0">至</span>
          <el-form-item prop="endTime" label-width="0px">
            <el-date-picker class="startTime_add" :picker-options="pickerOptionsStart" v-model="examineForm.endTime"
                            type="date" clearable placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-preventReClick @click="cancelExamineForm">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="updateInfo">确 认</el-button>
        <!--        <el-button v-preventReClick type="primary" @click="submitExamineForm">提 交</el-button>-->
      </div>
    </el-dialog>
    <!-- 添加或修改岗位对话框 -->
    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="title" :visible.sync="open"
               width="500px" append-to-body
    >
      <el-form @submit.native.prevent ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="级别名称" prop="levelName">
          <el-input v-model="form.levelName" placeholder="请输入级别名称" maxlength="20" show-word-limit/>
        </el-form-item>
        <el-form-item :label="`${$courseName ? $courseName : '课程'}数量`" prop="courseNum">
          <el-input-number :min="0" v-model="form.courseNum" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="教师数量" prop="teacherNum">
          <el-input-number :min="0" v-model="form.teacherNum" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="学生数量" prop="studentNum">
          <el-input-number :min="0" v-model="form.studentNum" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="储存空间" prop="storage">
          <div style="display: flex">
            <el-input-number :min="0" v-model="form.storage" placeholder="请输入"
                             style="margin-right: 10px"
            ></el-input-number>
            G
          </div>
        </el-form-item>
        <el-form-item label="级别简介">
          <el-input maxlength="500" show-word-limit :rows="4" v-model="form.content" type="textarea"
                    placeholder="请输入级别简介"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">

        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm">确 定</el-button>

      </div>
    </el-dialog>
    <!-- 查看驳回原因 -->
    <reject :type="3" :item="rejectItem" :open.sync="rejectOpen"></reject>
  </div>
</template>

<script>
import { listPost } from '@/api/school/level'
import adminDia from './component/adminDia'
import reject from '@/components/reject.vue'
import {
  listPost_school,
  delPost_school,
  addPost_school,
  updatePost_school
} from '@/api/school/list'

export default {
  name: 'Post',
  dicts: ['sys_normal_disable'],
  components: { adminDia, reject },
  data() {
    return {
      selectList: [],
      showExamineDialog: false,
      examineForm: {
        schoolId: '',
        levelId: '',
        startTime: '',
        endTime: ''
      },
      pickerOptionsStart: {
        // 时间只能大于startTime
        disabledDate: (time) => {
          if (!this.examineForm.startTime) {
            return
          }
          return time.getTime() < new Date(this.examineForm.startTime).getTime()
        }
      },
      pickerOptionsEnd: {
        // 时间只能小于startTime
        disabledDate: (time) => {
          if (!this.examineForm.endTime) {
            return
          }
          return time.getTime() > new Date(this.examineForm.endTime).getTime()
        }
      },
      examineFormRules: {
        levelId: [{ required: true, message: '级别不能为空', trigger: 'blur' }],
        startTime: [
          { required: true, message: '开始日期不能为空', trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: '结束日期不能为空', trigger: 'blur' }
        ]
      },
      //驳回原因组件参数
      rejectOpen: false,
      rejectItem: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // level
      level: [],
      // 查询参数
      activeIndex: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schoolName: undefined,
        level: undefined
      },
      levels: [
        { id: 1, name: '级别一' },
        { id: 2, name: '级别二' }
      ],
      // 表单参数
      form: {
        status: undefined,
        levelId: undefined,
        content: undefined,
        courseNum: undefined,
        studentNum: undefined,
        storage: undefined,
        teacherNum: undefined
      },
      // 表单校验
      rules: {
        levelName: [
          { required: true, message: '级别名称不能为空', trigger: 'blur' }
        ],
        courseNum: [
          {
            required: true,
            message: `${this.$courseName ? this.$courseName : '课程'
            }数量不能为空`,
            trigger: 'blur'
          }
        ],
        teacherNum: [
          { required: true, message: '教师数量不能为空', trigger: 'blur' }
        ],
        studentNum: [
          { required: true, message: '学生数量不能为空', trigger: 'blur' }
        ],
        storage: [{ required: true, message: '内存不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
    this.getLevel()
  },
  methods: {
    showExamine(row) {
      this.examineForm.schoolId = row.schoolId
      this.examineForm.schoolName = row.schoolName
      this.examineForm.schoolPic = row.schoolPic
      this.examineForm.startTime = row.startTime
      this.examineForm.endTime = row.endTime
      this.examineForm.levelId = row.levelId
      this.showExamineDialog = true
    },
    cancelExamineForm() {
      this.$refs['examineForm'].resetFields()
      this.showExamineDialog = false
    },
    updateInfo() {
      let params = {
        ...this.examineForm
      }
      updatePost_school(params).then(() => {
        this.$modal.msgSuccess('修改成功')
        this.showExamineDialog = false
        this.getList()
      })
    },
    handleReject(item) {
      item.uncheckReason = item.rejectReason
      this.rejectOpen = true
      this.rejectItem = item
    },
    // el-menu

    handleSelect(key) {
      this.queryParams.status = key
      this.getList()
    },
    // 停用 启用按钮
    handleSwitch(row) {
      let txt =
        row.status == 0
          ? `确定停用【${row.schoolName}】学校吗?停用后该学校所有的关联账号不可使用！`
          : `确认启用【${row.schoolName}】学校吗?启用后该学校可使用`
      this.$confirm(txt, `${row.status == 0 ? '停用确认' : '启用确认'}`, {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          let data = JSON.parse(JSON.stringify(row))
          data.status = row.status == 0 ? 1 : 0
          updatePost_school(data).then(() => {
            this.$modal.msgSuccess('操作成功')
            this.open = false
            this.getList()
          })
        })
        .catch(() => {
        })
    },
    /** level列表 */
    getLevel() {
      listPost({
        pageNum: 1,
        pageSize: 999
      }).then((response) => {
        this.level = response.rows
      })
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listPost_school(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        levelName: undefined,
        content: undefined,
        courseNum: undefined,
        studentNum: undefined,
        storage: undefined,
        teacherNum: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.schoolId)
      this.selectList = selection
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加级别";
      this.$router.push({ path: '/school/addSchool', query: { type: 'add' } })
    },
    /** 修改按钮操作 */
    handleUpdate(type, row) {
      let schoolId
      if (type == 0) {
        schoolId = this.ids[0]
      } else {
        schoolId = row.schoolId
      }
      this.$router.push({
        path: '/school/addSchool',
        query: { schoolId, type }
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.schoolId != undefined) {
            updatePost_school(this.form).then(() => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addPost_school(this.form).then(() => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const schoolIds = row.schoolId || this.ids
      this.$modal
        .confirm(
          `确认删除【${row.schoolName ? row.schoolName : this.selectList.length > 1 ? '这些' : this.selectList[0].schoolName}】学校吗？删除后不可恢复!`,
          `删除确认`,
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
        .then(function() {
          return delPost_school(schoolIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/post/export',
        {
          ...this.queryParams
        },
        `post_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
<style scoped lang="scss">
::v-deep .el-menu-item:hover {
  border-bottom: 1px solid #e6e6e6 !important;
}

::v-deep .el-menu--horizontal > .el-menu-item.is-active:hover {
  border-bottom: 2px solid #11a983 !important;
}

.startTime_add {
  width: 160px;
}

.level_add {
  width: 354px;
}
</style>
